{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 04-Numpy实战\n",
    "## 适合初学者快速入门的Numpy实战全集\n",
    "Numpy是一个用python实现的科学计算的扩展程序库，包括：\n",
    "* 1、一个强大的N维数组对象Array；\n",
    "* 2、比较成熟的（广播）函数库；\n",
    "* 3、用于整合C/C++和Fortran代码的工具包；\n",
    "* 4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。\n",
    "\n",
    "NumPy（Numeric Python）提供了许多高级的数值编程工具，如：矩阵数据类型、矢量处理，以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用，以及核心的科学计算组织如：Lawrence Livermore，NASA用其处理一些本来使用C++，Fortran或Matlab等所做的任务。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np   # 别名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Numpy创建array  数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1 一维array创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2 23  4]\n"
     ]
    }
   ],
   "source": [
    "# 一维array\n",
    "a = np.array([2,23,4], dtype=np.int32) # np.int默认为int32\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 多维array创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3 4]\n",
      " [3 4 5]\n",
      " [7 4 5]\n",
      " [6 2 1]] (4, 3)\n"
     ]
    }
   ],
   "source": [
    "# 多维array\n",
    "a = np.array([[2,3,4],\n",
    "              [3,4,5],\n",
    "              [7,4,5],\n",
    "              [6,2,1]\n",
    "             ])\n",
    "print(a, a.shape) # shape 数组的行数、列数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3 创建全零数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "a = np.zeros((3,4), dtype = np.int)   # 经常用在数组的初始化\n",
    "print(a) # 生成3行4列的全零矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.4 创建全1数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1]\n",
      " [1 1 1 1]\n",
      " [1 1 1 1]]\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "# 创建全一数据，同时指定数据类型\n",
    "a = np.ones((3,4),dtype=np.int)\n",
    "print(a)\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.5 创建全空数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7.55432630e-312 3.16202013e-322 0.00000000e+000 0.00000000e+000]\n",
      " [0.00000000e+000 9.96490526e-047 1.73224146e+185 8.69540732e-043]\n",
      " [1.08624449e-071 1.90286955e+185 1.91689759e-076 1.10660579e-046]]\n"
     ]
    }
   ],
   "source": [
    "# 创建全空数组，其实每个值都是接近于零的数\n",
    "a = np.empty((3,4))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.6 创建连续数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19 20 21] (12,)\n"
     ]
    }
   ],
   "source": [
    "# 创建连续数组 arange\n",
    "a = np.arange(10,22,1) # 10-20的数据，步长为2   不包含最后的数字\n",
    "print(a, a.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.7 创建线段型数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.          0.52631579  1.05263158  1.57894737  2.10526316  2.63157895\n",
      "  3.15789474  3.68421053  4.21052632  4.73684211  5.26315789  5.78947368\n",
      "  6.31578947  6.84210526  7.36842105  7.89473684  8.42105263  8.94736842\n",
      "  9.47368421 10.        ]\n"
     ]
    }
   ],
   "source": [
    "# 创建线段型数据 linspace\n",
    "a = np.linspace(0,10,20, endpoint = True) # 开始端1，结束端10，且分割成20个数据，生成线段\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 数组的基本属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of dim: 2 2\n",
      "shape: (2, 3)\n",
      "size: 6\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[2,3,4],\n",
    "              [3,4,5]] , dtype = np.int)\n",
    "# 维度\n",
    "print('number of dim:', a.ndim, a.shape[0])\n",
    "\n",
    "# 行数和列数\n",
    "print('shape:',a.shape)\n",
    "\n",
    "# 元素个数\n",
    "print('size:',a.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.Numpy索引与切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5 6 7 8 9]\n",
      "[[2 3 4]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(3,10)   # 不包含10\n",
    "b = np.array([[2,3,4],\n",
    "              [3,4,5]] , dtype = np.int)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "print(a[3:7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5]\n"
     ]
    }
   ],
   "source": [
    "print(b[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "print(b[1][2])\n",
    "print(b[1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4]\n"
     ]
    }
   ],
   "source": [
    "# list切片操作\n",
    "print(b[0,1:3]) # 1:3表示1-2不包含3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Numpy基本运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.1 一维矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 40] [1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "# 一维矩阵运算\n",
    "a = np.array([10,20,30,40])\n",
    "b = np.array([1,2,3,4])\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 21, 31, 41])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵和常数运算\n",
    "a + 1   # 1→[1,1,1,1](广播)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 40, 60, 80])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 2   # 2 → [2,2,2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加法： [11 22 33 44]\n",
      "减法： [ 9 18 27 36]\n",
      "元素相乘： [ 10  40  90 160]\n",
      "元素相除： [10. 10. 10. 10.]\n"
     ]
    }
   ],
   "source": [
    "# 逐项加减乘除 需要有相同的shape\n",
    "print(\"加法：\",a + b)\n",
    "print(\"减法：\",a - b)\n",
    "print(\"元素相乘：\",a * b)\n",
    "print(\"元素相除：\",a / b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  4  9 16]\n"
     ]
    }
   ],
   "source": [
    "# 在Numpy中，想要求出矩阵中各个元素的乘方需要依赖双星符号 **，以二次方举例，即：\n",
    "c = b**2\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.54402111  0.91294525 -0.98803162  0.74511316]\n",
      "[0.         0.69314718 1.09861229 1.38629436]\n"
     ]
    }
   ],
   "source": [
    "# Numpy中具有很多的数学函数工具\n",
    "print(np.sin(a))\n",
    "print(np.log(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False False False]\n"
     ]
    }
   ],
   "source": [
    "print(b<2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False False False]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,1,4,3])\n",
    "b = np.array([1,2,3,4])\n",
    "print(a==b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2 多维矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [0 1]]\n",
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,1],[0,1]])\n",
    "b = np.array([[1,2],[3,4]])\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [0, 4]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维矩阵加法： 逐项相加,需要a，b 行数、列数是相同\n",
    "a*b   # 元素的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [0 1]\n",
      " [2 2]] (3, 2)\n",
      "[[0]\n",
      " [1]] (2, 1)\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,1],[0,1],[2,2]])\n",
    "b = np.array([[0],[1]])\n",
    "print(a, a.shape)\n",
    "print(b, b.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [1]\n",
      " [2]] (3, 1)\n"
     ]
    }
   ],
   "source": [
    "# 多维度矩阵乘法\n",
    "# 矩阵相乘的第一种写法:\n",
    "c = a.dot(b)\n",
    "print(c, c.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [1]\n",
      " [2]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵相乘的第二种写法:\n",
    "c = np.dot(a,b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [1],\n",
       "       [2]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵相乘的第三种写法：\n",
    "a@b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (3,2) (2,1) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-52-2c8bc749c658>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mb\u001b[0m \u001b[1;31m# 多维矩阵乘法不能直接使用'*'号\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,2) (2,1) "
     ]
    }
   ],
   "source": [
    "a*b # 多维矩阵乘法不能直接使用'*'号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.3 数据统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[25 91 37 86]\n",
      " [24 68 49 22]]\n"
     ]
    }
   ],
   "source": [
    "# randint: 生成随机的整数\n",
    "a = np.random.randint(0,100,size = (2,4))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum   402\n",
      "mean   50.25\n",
      "中位数   43.0\n",
      "标准差   26.352182072837916\n",
      "方差   694.4375\n",
      "min   22\n",
      "max   91\n"
     ]
    }
   ],
   "source": [
    "print(\"sum  \",np.sum(a)) \n",
    "print(\"mean  \",np.mean(a))\n",
    "print(\"中位数  \",np.median(a))  # 22，24，25，37，49，68，86，91\n",
    "print(\"标准差  \",np.std(a))  # 标准差、\n",
    "print(\"方差  \",np.var(a))   # 方差、\n",
    "print(\"min  \",np.min(a))\n",
    "print(\"max  \",np.max(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值的索引号： 7\n",
      "最大值的索引号： 1\n"
     ]
    }
   ],
   "source": [
    "print(\"最小值的索引号：\", np.argmin(a))\n",
    "print(\"最大值的索引号：\", np.argmax(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你需要对行或者列进行查找运算，\n",
    "\n",
    "就需要在上述代码中为 axis 进行赋值。\n",
    "\n",
    "当axis的值为0的时候，将会以列作为查找单元，\n",
    "\n",
    "当axis的值为1的时候，将会以行作为查找单元。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum= [239 163]\n",
      "sum= [ 49 159  86 108]\n"
     ]
    }
   ],
   "source": [
    "# axis = 1  表示按行进行统计\n",
    "print(\"sum=\",np.sum(a,axis=1))\n",
    "print(\"sum=\",np.sum(a,axis=0))  # 按列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min= [24 68 37 22]\n",
      "[1 1 0 1]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(\"min=\",np.min(a,axis=0))  # 按列\n",
    "# 最小元素索引\n",
    "print(np.argmin(a,axis=0)) # 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max= [91 68]\n",
      "[1 1]\n"
     ]
    }
   ],
   "source": [
    "print(\"max=\",np.max(a,axis=1))  # 按行\n",
    "# 最大元素索引\n",
    "print(np.argmax(a,axis=1)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 数组的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.1 数组整形  **reshape**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19 20 21] (12,)\n"
     ]
    }
   ],
   "source": [
    "# 使用reshape改变上述数据的形状\n",
    "a = np.arange(10,22,1) # 10-20的数据，步长为2\n",
    "print(a, a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21]]\n",
      "[[10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]]\n"
     ]
    }
   ],
   "source": [
    "b = a.reshape((2,6), order = 'C')  # 按列来整形的话 order = 'F'\n",
    "c = a.reshape((3,4))\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19 20 21]\n",
      "[10 11 12 13 14 15 16 17 18 19 20 21]\n"
     ]
    }
   ],
   "source": [
    "print(b.flatten())   # 拍扁到一维，展平函数\n",
    "print(c.flatten())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.2 数据转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21]] (2, 6)\n",
      "[[10 16]\n",
      " [11 17]\n",
      " [12 18]\n",
      " [13 19]\n",
      " [14 20]\n",
      " [15 21]] (6, 2)\n"
     ]
    }
   ],
   "source": [
    "# 矩阵转置\n",
    "print(b,b.shape)\n",
    "d = np.transpose(b)\n",
    "print(d,d.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 16]\n",
      " [11 17]\n",
      " [12 18]\n",
      " [13 19]\n",
      " [14 20]\n",
      " [15 21]]\n"
     ]
    }
   ],
   "source": [
    "# 更常用的转置 a.T\n",
    "print(b.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.3 数组排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[84 34 89 84]\n",
      " [18 67 28 73]]\n"
     ]
    }
   ],
   "source": [
    "a = np.random.randint(0,100, (2,4))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后：\n",
      " [[34 84 84 89]\n",
      " [18 28 67 73]]\n"
     ]
    }
   ],
   "source": [
    "print(\"排序后：\\n\",np.sort(a))  # 默认是按照行来进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "展平排序后：\n",
      " [18 28 34 67 73 84 84 89]\n"
     ]
    }
   ],
   "source": [
    "print(\"展平排序后：\\n\",np.sort(a, axis = None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按列排序后：\n",
      " [[18 34 28 73]\n",
      " [84 67 89 84]]\n"
     ]
    }
   ],
   "source": [
    "print(\"按列排序后：\\n\",np.sort(a, axis = 0))  # 按列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "倒序排序：\n",
      " [[89 84 84 34]\n",
      " [73 67 28 18]]\n"
     ]
    }
   ],
   "source": [
    "# np.sort不支持倒序排序，所以只能负负得正 \n",
    "print(\"倒序排序：\\n\",-np.sort(-a , axis = 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.4 数组的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "# 第一种：=  赋值  （不推荐）\n",
    "a = np.arange(4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3] [0 1 2 3] [0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "# 赋值\n",
    "b = a\n",
    "c = a\n",
    "d = b\n",
    "print(b,c,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100   1   2   3] [100   1   2   3] [100   1   2   3] [100   1   2   3]\n"
     ]
    }
   ],
   "source": [
    "# =赋值，如果改变了原始的数组a， 由a衍生而来的bcd都会改变。\n",
    "a[0] = 100\n",
    "print(a,b,c,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二种： copy  （推荐）新生成的数组与原始数组没有关联。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(4)\n",
    "print(a) # [0 1 2 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "b = a.copy() # deep copy\n",
    "print(b) # [0 1 2 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100   1   2   3]\n",
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "a[0] = 100\n",
    "print(a) \n",
    "print(b) # [0 1 2 3]\n",
    "\n",
    "# 此时a与b已经没有关联"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. 组合数组\n",
    "- 使用hstack函数实现数组横向组合：np.hstack((arr1,arr2))\n",
    "- 使用vstack函数实现数组纵向组合：np.vstack((arr1,arr2))\n",
    "- 使用concatenate函数实现数组横向组合：np.concatenate((arr1,arr2),axis = 1))\n",
    "- 使用concatenate函数实现数组纵向组合：np.concatenate((arr1,arr2),axis = 0))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[0.  0.1 0.2 0.3 0.4]\n",
      " [0.5 0.6 0.7 0.8 0.9]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10).reshape(2, 5)\n",
    "b = np.linspace(0, 1, endpoint=False, num=10).reshape(2, 5)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. , 0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [5. , 6. , 7. , 8. , 9. , 0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a, b))   # 横向拼接，要求两个矩阵的行相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. ],\n",
       "       [5. , 6. , 7. , 8. , 9. ],\n",
       "       [0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a, b))  # 纵向拼接，要求两个矩阵的列数相同\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. ],\n",
       "       [5. , 6. , 7. , 8. , 9. ],\n",
       "       [0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a, b), axis=0)  # 纵向 == vstack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. , 0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [5. , 6. , 7. , 8. , 9. , 0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a, b), axis=1)  # 横向  == hstack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.47222992 0.14687638 0.14684961 0.00794174 0.09477946]\n",
      " [0.35508753 0.2219853  0.63513049 0.09705866 0.37568819]]\n"
     ]
    }
   ],
   "source": [
    "a = np.random.random(10).reshape(2, 5)  # np.random.random 生成（0，1）之间的数据\n",
    "print(a)\n",
    "np.savetxt('./tmp/tmp1.txt', a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.47222992 0.14687638 0.14684961 0.00794174 0.09477946]\n",
      " [0.35508753 0.2219853  0.63513049 0.09705866 0.37568819]]\n"
     ]
    }
   ],
   "source": [
    "b = np.loadtxt('./tmp/tmp1.txt')\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 课堂练习：\n",
    "- 读取iris数据集中的花萼长度数据（已保存为csv格式）\n",
    "- 对其进行排序、去重，\n",
    "- 并求出和、均值、标准差、方差、最小值、最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5.1 4.9 4.7 4.6 5.  5.4 4.6 5.  4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1\n",
      " 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.  5.  5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.\n",
      " 5.5 4.9 4.4 5.1 5.  4.5 4.4 5.  5.1 4.8 5.1 4.6 5.3 5.  7.  6.4 6.9 5.5\n",
      " 6.5 5.7 6.3 4.9 6.6 5.2 5.  5.9 6.  6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1\n",
      " 6.3 6.1 6.4 6.6 6.8 6.7 6.  5.7 5.5 5.5 5.8 6.  5.4 6.  6.7 6.3 5.6 5.5\n",
      " 5.5 6.1 5.8 5.  5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3\n",
      " 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.  6.9 5.6 7.7 6.3 6.7 7.2\n",
      " 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.  6.9 6.7 6.9 5.8 6.8\n",
      " 6.7 6.7 6.3 6.5 6.2 5.9] (150,)\n"
     ]
    }
   ],
   "source": [
    "data = np.loadtxt('./data/iris_sepal_length.csv')\n",
    "print(data, data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.  5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.\n",
      " 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.  7.1 7.2 7.3 7.4 7.6 7.7 7.9] (35,)\n"
     ]
    }
   ],
   "source": [
    "# 去重\n",
    "data = np.unique(data)\n",
    "print(data, data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.  5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.\n",
      " 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.  7.1 7.2 7.3 7.4 7.6 7.7 7.9]\n"
     ]
    }
   ],
   "source": [
    "# 排序\n",
    "data = np.sort(data)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum= 210.39999999999998\n",
      "mean= 6.011428571428571\n",
      "std= 1.0289443768310533\n",
      "var= 1.0587265306122449\n",
      "min= 4.3\n",
      "max= 7.9\n"
     ]
    }
   ],
   "source": [
    "print(\"sum=\", np.sum(data))\n",
    "print(\"mean=\", np.mean(data))  # 均值、\n",
    "print(\"std=\", np.std(data))  # 标准差、\n",
    "print(\"var=\", np.var(data))  # 方差、\n",
    "print(\"min=\", np.min(data))  # 最小值、\n",
    "print(\"max=\", np.max(data))  # 最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "286.001px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
